package config

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"modbus/device/analysis"
	"time"
)

type Result struct {
	v int
}

type Info struct {
	result chan Result
	a      int
}

var g_table *analysis.DevInfoTable

func arrbyteInit(bytes []byte) {
	for i, _ := range bytes {
		bytes[i] = 0xff
	}
}

func arrUint16Init(Ubuf []uint16) {
	for i, _ := range Ubuf {
		Ubuf[i] = 0xffff
	}
}
func CollenDev(cfgFile string) (*analysis.DevInfoTable, error) {
	//file, err := excelize.OpenFile("寄存器表.xlsx")
	file, err := excelize.OpenFile(cfgFile)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	g_table = &analysis.DevInfoTable{}
	g_table.RegTable.CoisReg = make(map[int]analysis.DevRegister, 65536)
	g_table.RegTable.DiscretInput = make(map[int]analysis.DevRegister, 65536)
	g_table.RegTable.HoldingReg = make(map[int]analysis.DevRegister, 65536)
	g_table.RegTable.InputReg = make(map[int]analysis.DevRegister, 65536)

	g_table.RegBufTable.DiscreteInputs = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufTable.DiscreteInputs)
	g_table.RegBufTable.Coils = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufTable.Coils)
	g_table.RegBufTable.HoldingRegisters = make([]uint16, 65536)
	//arrUint16Init(g_table.RegBufTable.HoldingRegisters)
	g_table.RegBufTable.InputRegisters = make([]uint16, 65536)
	//arrUint16Init(g_table.RegBufTable.InputRegisters)
	g_table.RegBufStateTable.DiscreteInputs = make([]byte, 65536)
	arrbyteInit(g_table.RegBufStateTable.DiscreteInputs)
	g_table.RegBufStateTable.Coils = make([]byte, 65536)
	arrbyteInit(g_table.RegBufStateTable.Coils)
	g_table.RegBufStateTable.HoldingRegisters = make([]uint16, 65536)
	arrUint16Init(g_table.RegBufStateTable.HoldingRegisters)
	g_table.RegBufStateTable.InputRegisters = make([]uint16, 65536)
	arrUint16Init(g_table.RegBufStateTable.InputRegisters)

	g_table.RegBufStateTable.DiscreteState = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufStateTable.DiscreteState)
	g_table.RegBufStateTable.CoilsState = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufStateTable.CoilsState)
	g_table.RegBufStateTable.HoldingState = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufStateTable.HoldingState)
	g_table.RegBufStateTable.InputState = make([]byte, 65536)
	//arrbyteInit(g_table.RegBufStateTable.InputState)
	rows := file.GetRows("接口")
	for i, row := range rows {
		fmt.Println(i, row, len(row))
		if i == 0 {
			continue
		}
		strings := analysis.DevAnalysis_strings(row)
		fmt.Println(strings)
		g_table.DevInfo = strings
	}
	rows = file.GetRows("遥信")
	for i, row := range rows {
		fmt.Println(i, row)
		if i == 0 {
			continue
		}
		strings := analysis.DevRegAnalysis_strings(row)
		fmt.Println(strings)
		if _, ok := g_table.RegTable.DiscretInput[strings.RegAddr]; ok {
			register := g_table.RegTable.DiscretInput[strings.RegAddr]
			register.ListTable = append(register.ListTable, strings)
			g_table.RegTable.DiscretInput[strings.RegAddr] = register
		} else {
			g_table.RegTable.DiscretInput[strings.RegAddr] = strings
		}
		//g_table.RegTable = append(g_table.RegTable, strings)
	}

	rows = file.GetRows("遥测")
	for i, row := range rows {
		fmt.Println(i, row)
		if i == 0 {
			continue
		}
		strings := analysis.DevRegAnalysis_strings(row)
		fmt.Println(strings)
		if _, ok := g_table.RegTable.InputReg[strings.RegAddr]; ok {
			register := g_table.RegTable.InputReg[strings.RegAddr]
			register.ListTable = append(register.ListTable, strings)
			g_table.RegTable.InputReg[strings.RegAddr] = register
		} else {
			g_table.RegTable.InputReg[strings.RegAddr] = strings
		}
		//g_table.RegTable = append(g_table.RegTable, strings)
	}

	rows = file.GetRows("遥控")
	for i, row := range rows {
		fmt.Println(i, row)
		if i == 0 {
			continue
		}
		strings := analysis.DevRegAnalysis_strings(row)
		fmt.Println(strings)
		if _, ok := g_table.RegTable.CoisReg[strings.RegAddr]; ok {
			register := g_table.RegTable.CoisReg[strings.RegAddr]
			register.ListTable = append(register.ListTable, strings)
			g_table.RegTable.CoisReg[strings.RegAddr] = register
		} else {
			g_table.RegTable.CoisReg[strings.RegAddr] = strings
		}
		//g_table.RegTable = append(g_table.RegTable, strings)
	}

	rows = file.GetRows("遥调")
	for i, row := range rows {
		fmt.Println(i, row)
		if i == 0 {
			continue
		}
		strings := analysis.DevRegAnalysis_strings(row)
		fmt.Println(strings)
		//g_table.RegTable = append(g_table.RegTable, strings)
		if _, ok := g_table.RegTable.HoldingReg[strings.RegAddr]; ok {
			register := g_table.RegTable.HoldingReg[strings.RegAddr]
			register.ListTable = append(register.ListTable, strings)
			g_table.RegTable.HoldingReg[strings.RegAddr] = register
		} else {
			g_table.RegTable.HoldingReg[strings.RegAddr] = strings
		}
	}

	rows = file.GetRows("采集表")
	for i, row := range rows {
		fmt.Println(i, row)
		if i == 0 {
			continue
		}
		strings := analysis.Collect_Analysis_strings(row)
		g_table.Collect = append(g_table.Collect, strings)
	}

	return g_table, nil
}

func Sendinfo(In chan Info) {
	result := Result{}
	result.v = 10
	for {
		select {
		case info := <-In:
			fmt.Println(info)
			info.result <- result
		}
		time.Sleep(time.Second)
	}

}
